//
//  ShortListViewController.m
//  Commerce
//
//  Created by xiaofeifei on 15/2/17.
//  Copyright (c) 2015年 fei. All rights reserved.
//

#import "ShortListViewController.h"
#import "ShortDetailViewController.h"
#import "OwnerdetailViewController.h"
#import "ShortMapViewController.h"
#import "DOPDropDownMenu.h"
#import "ShortCell.h"

@interface ShortListViewController ()<DOPDropDownMenuDataSource, DOPDropDownMenuDelegate>{
    NSMutableArray *_dataArray;
    NSArray *_specificationArray;
    NSArray *_typeArray;
    NSArray *_distanceArray;
    NSString *_line;
    BOOL _hasLoad;
    NSString *_specificationStr;
    NSString *_typeStr;
}

@end

@implementation ShortListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navStr = @"短途配送";
    _dataArray = [[NSMutableArray alloc] initWithCapacity:10];
    _line = @"0";
    _specificationStr =@"";
    _typeStr =@"";
    [self refreshData];

    _specificationArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"SpecificationData"];
    _typeArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"TypeData"];
    if (_specificationArray.count == 0) {
        [self getSpecificationData];
    }
    if (_typeArray.count == 0) {
        [self getTypeData];
    }
    _distanceArray = @[@"1公里",@"3公里",@"5公里",@"10公里",@"全城"];

    DOPDropDownMenu *menu = [[DOPDropDownMenu alloc] initWithOrigin:CGPointMake(0, self.navView.frame.size.height) andHeight:40];
    menu.dataSource = self;
    menu.delegate = self;
    [self.view addSubview:menu];
    
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    if (_hasLoad==NO) {
        self.tableView.frame =CGRectMake(0,self.navView.frame.size.height + 40, self.view.frame.size.width, self.view.frame.size.height - (self.navView.frame.size.height + 40));
        
        UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 70, self.navView.frame.size.height-38, 60, 30)];
        [rightBtn setTitle:@"地图" forState:UIControlStateNormal];
        [rightBtn setBackgroundImage:[UIImage imageNamed:@"back_n.png"] forState:UIControlStateNormal];
        [rightBtn addTarget:self action:@selector(gotoMapList:) forControlEvents:UIControlEventTouchUpInside];
        [self.navView addSubview:rightBtn];
        
        _hasLoad = YES;
    }
}


- (void)gotoMapList:(id)sender{
    ShortMapViewController *vc = [[ShortMapViewController alloc] initWithNibName:nil bundle:nil];
    vc.annotationList = _dataArray;
    [self presentViewController:vc animated:YES completion:^{}];
}

- (void)getDataList{
    [self getSpeclalLineList];
}

#pragma mark -请求接口
- (void)getSpeclalLineList{//专线列表
    [SVProgressHUD showWithStatus:@"加载中" maskType:SVProgressHUDMaskTypeBlack];
    //传入的参数
    NSMutableDictionary *parameters = [GlobalData paramterDic];
    CLLocationCoordinate2D uselocation = [[GlobalData sharedGlobalData] uselocation];
    [parameters addEntriesFromDictionary:@{
                                           @"page_index":[NSString stringWithFormat:@"%d",self.page],
                                           @"page_size":[NSString stringWithFormat:@"%d",self.pageLimit],
                                           @"car_spec":_specificationStr,
                                           @"car_type":_typeStr,
                                           @"dong_jing":[NSString stringWithFormat:@"%f",uselocation.longitude],
                                           @"bei_wei":[NSString stringWithFormat:@"%f",uselocation.latitude],
                                           @"line_num":_line}];
    [[BaseNetwork alloc] initWithMethodUrl:[NSString stringWithFormat:@"%@car/short",URL_ROOT]
                                    params:parameters
                               requestMode:DGHTTPMethodPOST
                               loadingShow:YES
                              didFinshLoad:^(id result) {
                                  id responseObject = result;
                                  NSLog(@"responseObject=%@",responseObject);
                                  if ([(NSArray *)responseObject count] < self.pageLimit) {
                                      self.canLoadMore = NO;
                                  }else{
                                      self.canLoadMore = YES;
                                  }
                                  if (self.page == 1) {
                                      [self.tableView setContentOffset:CGPointZero animated:NO];
                                      [_dataArray removeAllObjects];
                                      [_dataArray addObjectsFromArray:responseObject];
                                      [self addItemsOnTop];
                                  }else{
                                      [_dataArray addObjectsFromArray:responseObject];
                                      [self addItemsOnBottom];
                                  }
                                  
                                  
                              }];
}

- (void)getSpecificationData{
    [SVProgressHUD showWithStatus:@"加载中" maskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parameters = [GlobalData paramterDic];
    [[BaseNetwork alloc] initWithMethodUrl:[NSString stringWithFormat:@"%@sys/carspec",URL_ROOT]
                                    params:parameters
                               requestMode:DGHTTPMethodPOST
                               loadingShow:YES
                              didFinshLoad:^(id result) {
                                  id responseObject = result;
                                  NSMutableArray *temp = [[NSMutableArray alloc] init];
                                  [temp addObject:@{@"Code":@"0",@"CodeID":@"0",@"CodeType":@"0",@"Text":@"不限制"}];
                                  [temp addObjectsFromArray:responseObject];
                                  _specificationArray = temp;
                                  [[NSUserDefaults standardUserDefaults] setObject:_specificationArray forKey:@"SpecificationData"];
                                  [SVProgressHUD dismiss];
                                  
                              }];
}

- (void)getTypeData{
    [SVProgressHUD showWithStatus:@"加载中" maskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parameters = [GlobalData paramterDic];
    [[BaseNetwork alloc] initWithMethodUrl:[NSString stringWithFormat:@"%@sys/cartype",URL_ROOT]
                                    params:parameters
                               requestMode:DGHTTPMethodPOST
                               loadingShow:YES
                              didFinshLoad:^(id result) {
                                  id responseObject = result;
                                  NSMutableArray *temp = [[NSMutableArray alloc] init];
                                  [temp addObject:@{@"Code":@"0",@"CodeID":@"0",@"CodeType":@"0",@"Text":@"不限制"}];
                                  [temp addObjectsFromArray:responseObject];
                                  _typeArray = temp;
                                  [[NSUserDefaults standardUserDefaults] setObject:_typeArray forKey:@"TypeData"];
                                  [SVProgressHUD dismiss];
                                  
                              }];

}

#pragma mark - Table View Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 82;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellIdentifier = @"Cell";
    
    ShortCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"ShortCell" owner:self options:nil];
        cell = (ShortCell *)[array objectAtIndex:0];
    }
    [cell configUIWithData:_dataArray[indexPath.row]];
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    OwnerdetailViewController *vc = [[OwnerdetailViewController alloc] initWithNibName:nil bundle:nil];
    vc.carId =_dataArray[indexPath.row][@"CarID"];
    [(UINavigationController *)self.view.window.rootViewController pushViewController:vc animated:YES];

//    ShortDetailViewController *vc = [[ShortDetailViewController alloc] initWithNibName:nil bundle:nil];
//    vc.dataDic = _dataArray[indexPath.row];
//    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -- dropDownListDelegate
- (NSInteger)numberOfColumnsInMenu:(DOPDropDownMenu *)menu {
    return 3;
}

- (NSInteger)menu:(DOPDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column {
    switch (column) {
        case 0:
            return _typeArray.count;
            break;
        case 1:
            return _specificationArray.count;
            break;
        case 2:
            return _distanceArray.count;
            break;
        default:
            return 0;
            break;
    }
}

- (NSString *)menu:(DOPDropDownMenu *)menu titleForRowAtIndexPath:(DOPIndexPath *)indexPath {
    switch (indexPath.column) {
        case 0: return _typeArray[indexPath.row][@"Text"];
            break;
        case 1: return _specificationArray[indexPath.row][@"Text"];
            break;
        case 2: return _distanceArray[indexPath.row];
            break;
        default:
            return nil;
            break;
    }
}

- (void)menu:(DOPDropDownMenu *)menu didSelectRowAtIndexPath:(DOPIndexPath *)indexPath {
    NSLog(@"column:%li row:%li", (long)indexPath.column, (long)indexPath.row);
    NSLog(@"%@",[menu titleForRowAtIndexPath:indexPath]);
    switch (indexPath.column) {
        case 0:
            _typeStr =[menu titleForRowAtIndexPath:indexPath];
            break;
        case 1:
            _specificationStr =[menu titleForRowAtIndexPath:indexPath];
            break;
        case 2:
            if (indexPath.row == _distanceArray.count -1) {
                _line = @"0";
            }else{
                _line =[[menu titleForRowAtIndexPath:indexPath] stringByReplacingOccurrencesOfString:@"公里" withString:@""];
            }
            break;
            
        default:
            break;
    }
    [self refreshData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
